﻿using JMcms.Common;
using JMcms.DBUtility;
using JMcms.Web.Plugin.Job.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace JMcms.Web.Plugin.Job.DAL
{
   public class job
    {
        private string databaseprefix;
        public job(string _databaseprefix)
        {
            this.databaseprefix = _databaseprefix;
        }
        public bool Exists(int id)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("select count(1) from " + this.databaseprefix + "job");
            stringBuilder.Append(" where id=@id");
            SqlParameter[] array = new SqlParameter[]
            {
                new SqlParameter("@id", SqlDbType.Int, 4)
            };
            array[0].Value = id;
            return DbHelperSQL.Exists(stringBuilder.ToString(), array);
        }
        public int Add(JMcms.Web.Plugin.Job.Model.job model)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("insert into " + this.databaseprefix + "job(");
            stringBuilder.Append("title,work_address,salary,experience,education,description,sort_id,is_lock,add_time)");
            stringBuilder.Append(" values (");
            stringBuilder.Append("@title,@work_address,@salary,@experience,@education,@description,@sort_id,@is_lock,@add_time)");
            SqlParameter[] array = new SqlParameter[]
            {
                new SqlParameter("@title", SqlDbType.NVarChar, 100),
                new SqlParameter("@work_address", SqlDbType.NVarChar, 100),
                new SqlParameter("@salary", SqlDbType.NVarChar, 100),
                new SqlParameter("@experience", SqlDbType.NVarChar, 30),
                new SqlParameter("@education", SqlDbType.NVarChar, 30),
                new SqlParameter("@description", SqlDbType.NVarChar, 255),
                new SqlParameter("@sort_id", SqlDbType.Int, 4),
                new SqlParameter("@is_lock", SqlDbType.Int, 4),
                new SqlParameter("@add_time", SqlDbType.DateTime)
            };
            array[0].Value = model.title;
            array[1].Value = model.work_address;
            array[2].Value = model.salary;
            array[3].Value = model.experience;
            array[4].Value = model.education;
            array[5].Value = model.description;
            array[6].Value = model.sort_id;
            array[7].Value = model.is_lock;
            array[8].Value = model.add_time;
            return DbHelperSQL.ExecuteSql(stringBuilder.ToString(), array);
        }
        public bool Update(JMcms.Web.Plugin.Job.Model.job model)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("update " + this.databaseprefix + "job set ");
            stringBuilder.Append("title=@title,");
            stringBuilder.Append("work_address=@work_address,");
            stringBuilder.Append("salary=@salary,");
            stringBuilder.Append("experience=@experience,");
            stringBuilder.Append("education=@education,");
            stringBuilder.Append("description=@description,");
            stringBuilder.Append("sort_id=@sort_id,");
            stringBuilder.Append("is_lock=@is_lock,");
            stringBuilder.Append("add_time=@add_time");
            stringBuilder.Append(" where id=@id");
            SqlParameter[] array = new SqlParameter[]
            {
                new SqlParameter("@title", SqlDbType.NVarChar, 100),
                new SqlParameter("@work_address", SqlDbType.NVarChar, 100),
                new SqlParameter("@salary", SqlDbType.NVarChar, 100),
                new SqlParameter("@experience", SqlDbType.NVarChar, 30),
                new SqlParameter("@education", SqlDbType.NVarChar, 30),
                new SqlParameter("@description", SqlDbType.NVarChar, 255),
                new SqlParameter("@sort_id", SqlDbType.Int, 4),
                new SqlParameter("@is_lock", SqlDbType.Int, 4),
                new SqlParameter("@add_time", SqlDbType.DateTime),
                new SqlParameter("@id", SqlDbType.Int, 4)
            };
            array[0].Value = model.title;
            array[1].Value = model.work_address;
            array[2].Value = model.salary;
            array[3].Value = model.experience;
            array[4].Value = model.education;
            array[5].Value = model.description;
            array[6].Value = model.sort_id;
            array[7].Value = model.is_lock;
            array[8].Value = model.add_time;
            array[9].Value = model.id;
            int num = DbHelperSQL.ExecuteSql(stringBuilder.ToString(), array);
            return num > 0;
        }
        public bool UpdateField(int id, string strValue)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("update " + this.databaseprefix + "job set " + strValue);
            stringBuilder.Append(" where id=" + id);
            int num = DbHelperSQL.ExecuteSql(stringBuilder.ToString());
            return num > 0;
        }
        public bool Delete(int id)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("delete from " + this.databaseprefix + "job ");
            stringBuilder.Append(" where id=@id");
            SqlParameter[] array = new SqlParameter[]
            {
                new SqlParameter("@id", SqlDbType.Int, 4)
            };
            array[0].Value = id;
            int num = DbHelperSQL.ExecuteSql(stringBuilder.ToString(), array);
            return num > 0;
        }
        public JMcms.Web.Plugin.Job.Model.job GetModel(int id)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("select id,title,work_address,salary,experience,education,description,sort_id,is_lock,add_time from " + this.databaseprefix + "job ");
            stringBuilder.Append(" where id=@id");
            SqlParameter[] array = new SqlParameter[]
            {
                new SqlParameter("@id", SqlDbType.Int, 4)
            };
            array[0].Value = id;
            JMcms.Web.Plugin.Job.Model.job job = new JMcms.Web.Plugin.Job.Model.job();
            DataSet dataSet = DbHelperSQL.Query(stringBuilder.ToString(), array);
            bool flag = dataSet.Tables[0].Rows.Count > 0;
            JMcms.Web.Plugin.Job.Model.job result;
            if (flag)
            {
                result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
            }
            else
            {
                result = null;
            }
            return result;
        }
        public JMcms.Web.Plugin.Job.Model.job DataRowToModel(DataRow row)
        {
            JMcms.Web.Plugin.Job.Model.job job = new JMcms.Web.Plugin.Job.Model.job();
            bool flag = row != null;
            if (flag)
            {
                bool flag2 = row["id"] != null && row["id"].ToString() != "";
                if (flag2)
                {
                    job.id = int.Parse(row["id"].ToString());
                }
                bool flag3 = row["title"] != null;
                if (flag3)
                {
                    job.title = row["title"].ToString();
                }
                bool flag4 = row["work_address"] != null;
                if (flag4)
                {
                    job.work_address = row["work_address"].ToString();
                }
                bool flag5 = row["salary"] != null;
                if (flag5)
                {
                    job.salary = row["salary"].ToString();
                }
                bool flag6 = row["experience"] != null;
                if (flag6)
                {
                    job.experience = row["experience"].ToString();
                }
                bool flag7 = row["education"] != null;
                if (flag7)
                {
                    job.education = row["education"].ToString();
                }
                bool flag8 = row["description"] != null;
                if (flag8)
                {
                    job.description = row["description"].ToString();
                }
                bool flag9 = row["sort_id"] != null && row["sort_id"].ToString() != "";
                if (flag9)
                {
                    job.sort_id = new int?(int.Parse(row["sort_id"].ToString()));
                }
                bool flag10 = row["is_lock"] != null && row["is_lock"].ToString() != "";
                if (flag10)
                {
                    job.is_lock = new int?(int.Parse(row["is_lock"].ToString()));
                }
                bool flag11 = row["add_time"] != null && row["add_time"].ToString() != "";
                if (flag11)
                {
                    job.add_time = DateTime.Parse(row["add_time"].ToString());
                }
            }
            return job;
        }
        public DataSet GetList(string strWhere)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("select id,title,work_address,salary,experience,education,description,sort_id,is_lock,add_time ");
            stringBuilder.Append(" FROM " + this.databaseprefix + "job ");
            bool flag = strWhere.Trim() != "";
            if (flag)
            {
                stringBuilder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(stringBuilder.ToString());
        }
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("select ");
            bool flag = Top > 0;
            if (flag)
            {
                stringBuilder.Append(" top " + Top.ToString());
            }
            stringBuilder.Append(" id,title,work_address,salary,experience,education,description,sort_id,is_lock,add_time ");
            stringBuilder.Append(" FROM " + this.databaseprefix + "job ");
            bool flag2 = strWhere.Trim() != "";
            if (flag2)
            {
                stringBuilder.Append(" where " + strWhere);
            }
            stringBuilder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(stringBuilder.ToString());
        }
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("select * FROM " + this.databaseprefix + "job");
            bool flag = strWhere.Trim() != "";
            if (flag)
            {
                stringBuilder.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(stringBuilder.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, stringBuilder.ToString(), filedOrder));
        }
    }
}
